package com.muy.redis.view.window.redis.bean;

import com.google.common.base.Objects;
import com.intellij.util.xmlb.annotations.Transient;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.Serializable;
import java.util.Map;

/**
 * @Author jiyanghuang
 * @Date 2023/9/15 20:20
 */
@EqualsAndHashCode(callSuper = false)
@Data
public class MrRedisConnectionParam implements Serializable {

    private String id;

    private String name;

    private String url;

    private String port;

    private String user;

    @Transient
    private String password;

    /**
     * 每个db的分组标识
     */
    private Map<Integer, String> groupSymbols;

    /**
     * 是否全局配置
     */
    private Boolean global;

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MrRedisConnectionParam that = (MrRedisConnectionParam) o;
        return Objects.equal(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public String getPort() {
        return port;
    }

    public String getUser() {
        return user;
    }

    @Transient
    public String getPassword() {
        return password;
    }

    public Map<Integer, String> getGroupSymbols() {
        return groupSymbols;
    }

    public Boolean getGlobal() {
        return global;
    }

    public String genId() {
        return DigestUtils.md5Hex(String.valueOf(System.currentTimeMillis()));
    }

    public MrRedisConnectionParam copyThis() {
        MrRedisConnectionParam mrRedisConnectionParam = new MrRedisConnectionParam();
        mrRedisConnectionParam.setId(this.genId());
        mrRedisConnectionParam.setName(this.getName());
        mrRedisConnectionParam.setUrl(this.getUrl());
        mrRedisConnectionParam.setPort(this.getPort());
        mrRedisConnectionParam.setUser(this.getUser());
        mrRedisConnectionParam.setPassword(this.getPassword());
        mrRedisConnectionParam.setGroupSymbols(this.getGroupSymbols());
        mrRedisConnectionParam.setGlobal(this.getGlobal());
        return mrRedisConnectionParam;
    }
}
